package screencharthandler

import (
	"github.com/gin-gonic/gin"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper/request"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
)

// HandleGetPlacesInfo get-places-info 查询网点/设备数据
func HandleGetPlacesInfo(ctx *gin.Context) {
	data := dao.NewStatisticalScreenDao().GetPlacesInfo()
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleGetPlacesTypeRatio 门店占比查询
func HandleGetPlacesTypeRatio(ctx *gin.Context) {
	data := dao.NewStatisticalScreenDao().GetPlacesTypeRatio()
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleGetPlacesGeography 门店地理分布查询
func HandleGetPlacesGeography(ctx *gin.Context) {
	data := dao.NewStatisticalScreenDao().GetPlacesGeography()
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleGetPayMonth 总流水月统计
func HandleGetPayMonth(ctx *gin.Context) {
	param := &request.ScreenGetPayMonthChartReq{}
	//dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	data := dao.NewStatisticalScreenDao().GetPayMonthChart(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleGetOrderList 查询订单列表
func HandleGetOrderList(ctx *gin.Context) {
	param := &request.ScreenGetOrderListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	data := dao.NewStatisticalScreenDao().GetStatisticsOrderList(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleGetStatisticsInfo 查询统计数据
func HandleGetStatisticsInfo(ctx *gin.Context) {
	maxId := dao.NewStatisticalScreenDao().GetStatisticsMaxOrderId().Unwrap().(int)

	mData := dao.NewStatisticalScreenDao().GetStatisticsInfoTodayEndMoney(maxId)
	oData := dao.NewStatisticalScreenDao().GetStatisticsInfoTodayCreateOrderNum(maxId)
	if mData.Err != nil || oData.Err != nil {
		Response(ctx)("", CodeInternalServerError, nil)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"payMoney": mData.Unwrap().(*request.ScreenStatisticalDayInfoRes).PayMoney,
		"orderNum": oData.Unwrap().(*request.ScreenStatisticalDayInfoRes).OrderNum,
	})(OK)
}
